/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Message;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.MemberRank;
import com.hboxs.asl.service.MemberRankService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * Controller - 会员等级
 */
@Controller("adminMemberRankController")
@RequestMapping("/asl-admin/member_rank")
public class MemberRankController extends BaseController {

    @Resource(name = "memberRankServiceImpl")
    private MemberRankService memberRankService;

    /**
     * 检查名称是否唯一
     */
    @RequestMapping(value = "/check_name", method = RequestMethod.GET)
    public
    @ResponseBody
    boolean checkName(String previousName, String name) {
        if (StringUtils.isEmpty(name)) {
            return false;
        }
        return memberRankService.nameUnique(previousName, name);
    }

    /**
     * 检查消费金额是否唯一
     */
    @RequestMapping(value = "/check_amount", method = RequestMethod.GET)
    public
    @ResponseBody
    boolean checkAmount(BigDecimal previousAmount, BigDecimal amount) {
        if (amount == null) {
            return false;
        }
        return memberRankService.amountUnique(previousAmount, amount);
    }

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model) {
        return "/admin/member_rank/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(MemberRank memberRank, RedirectAttributes redirectAttributes) {
        if (!isValid(memberRank)) {
            return ERROR_VIEW;
        }
        if (memberRankService.nameExists(memberRank.getName())) {
            return ERROR_VIEW;
        }
        memberRank.setIsSpecial(false);
        /**
        if (memberRank.getIsSpecial()) {
            memberRank.setAmount(null);
        } else if (memberRank.getAmount() == null || memberRankService.amountExists(memberRank.getAmount())) {
            return ERROR_VIEW;
        }
        */
        memberRank.setMembers(null);
        memberRankService.save(memberRank);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        model.addAttribute("memberRank", memberRankService.find(id));
        return "/admin/member_rank/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(MemberRank memberRank, RedirectAttributes redirectAttributes) {
        MemberRank pMemberRank = memberRankService.find(memberRank.getId());
        if (pMemberRank == null) {
            return ERROR_VIEW;
        }
        if (!memberRankService.nameUnique(pMemberRank.getName(), memberRank.getName())) {
            return ERROR_VIEW;
        }
        if (pMemberRank.getIsDefault()) {
            memberRank.setIsDefault(true);
        }
        memberRank.setIsSpecial(false);
        /**
        if (memberRank.getIsSpecial()) {
            memberRank.setAmount(null);
        } else if (memberRank.getAmount() == null || !memberRankService.amountUnique(pMemberRank.getAmount(), memberRank.getAmount())) {
            return ERROR_VIEW;
        }
        */
        memberRankService.update(memberRank, "members", "promotions");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model) {
        model.addAttribute("page", memberRankService.findPage(pageable));
        return "/admin/member_rank/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        if (ids != null) {
            for (Long id : ids) {
                MemberRank memberRank = memberRankService.find(id);
                if (memberRank != null && memberRank.getMembers() != null && !memberRank.getMembers().isEmpty()) {
                    return Message.error("admin.memberRank.deleteExistNotAllowed", memberRank.getName());
                }
            }
            long totalCount = memberRankService.count();
            if (ids.length >= totalCount) {
                return Message.error("admin.common.deleteAllNotAllowed");
            }
            memberRankService.delete(ids);
        }
        return SUCCESS_MESSAGE;
    }

}